<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/9 0009
 * Time: 16:03
 */
namespace app\admin\controller;
use app\admin\model\Forward;
use app\admin\model\News;
use app\admin\model\User;
use think\Db;
use think\Request;

class Page
{
    /**课程分支分页
     * @param Request $request
     * @return array
     */
    public function branch(Request $request){
        $limit = $request->param('limit',10);
        $page  = $request->param('page','1');
        $tiaojian = $request->param('tiaojian',null);
        if($tiaojian){
            $data = Db::view('classes','*')
                ->view('branch','*','branch.tables_class=classes.id','right')
                ->where('branch.branch_name','like',"%$tiaojian%")
                ->whereOr('classes.class_name','like',"%$tiaojian%")
                ->order('branch.tables_class desc')
                ->limit(($page-1)*$limit,$limit)
                ->select();
        }else{
            $data = Db::view('classes','*')
                ->view('branch','*','branch.tables_class=classes.id','right')
                ->order('branch.tables_class desc')
                ->limit(($page-1)*$limit,$limit)
                ->select();
        }
        return $data;
    }

//    /**题目分页
//     * @param Request $request
//     * @return array|\PDOStatement|string|\think\Collection
//     */
//    public function question_bank(Request $request){
//        $three_level_id = $request->param('three_level_id',null);
//        $limit    = $request->param('limit',10);
//        $page     = $request->param('page',1);
//        $branch = Db::name('branch')->where('id',$branchid)->find();
//        return Db::name($branch['table_name'])->json(["answer"])->order('lanmu desc')->limit(($page-1)*$limit,$limit)->select();
//    }
    /**题目分页
     * @param $three_level_id:第三级分类
     * @param $limit:每页长度
     * @param $page：页数
     * @return array|\PDOStatement|string|\think\Collection
     */
public function question_bank($three_level_id,$limit=10,$page=1){
    if(!$three_level_id){
        return false;
    }
        $data =  Db::name('bank')
            ->where('three_level_id',$three_level_id)
            ->where('status','<>',2)
            ->json(['answer'])
            ->order('type desc')
            ->limit(($page-1)*$limit,$limit)
            ->select();
        $datarow = Db::name('bank')
            ->where('three_level_id',$three_level_id)
            ->where('status','<>',2)
            ->count();
        return ['row'=>$datarow,'data'=>$data];
}

    /**
     * @param Request $request
     * @return \think\response\Json
     */
    public function question_banks(Request $request){
        $three_level_id = $request->param('three_levelid',null);
        $limit = $request->param('limit',10);
        $page  = $request->param('page',1);
        $data = $this->question_bank($three_level_id,$limit,$page);
//        return $data;
        if(!$data){
            return json(['code'=>203,'msg'=>'没有数据了']);
        }
        $type = Db::name('situation')->select();
        foreach ($data['data'] as $key=>$value){
            foreach ($type as $values){
                if($values['id']==$value['type']){
                    $data['data'][$key]['type'] = $values['situation_name'];
                }
            }
        }
        return json(['code'=>200,'data'=>$data['data']]);
    }

    /**等级分页
     * @param Request $request
     * @return array|\PDOStatement|string|\think\Collection
     */
    public function grade(Request $request){
        $limit = $request->param('limit',10);
        $page  = $request->param('page',1);
        $tiaojian = $request->param('tiaojian',null);
        if($tiaojian){
            $data = Db::name('grade')->where('number','like',"%$tiaojian%")->order('number asc')->limit(($page-1)*$limit,$limit)->select();
        }else{
            $data = Db::name('grade')->order('number asc')->limit(($page-1)*$limit,$limit)->select();
        }
        return $data;
    }

    /**未解题目分页
     * @param Request $request
     * @return \think\response\Json
     */
    public function weijietiku(Request $request){
        $limit = $request->param('limit',10);
        $page  = $request->param('page',1);
        $data = Db::view('bank',true)
            ->view('classes','class_name','bank.classes_id=classes.id','left')
            ->view('branch','branch_name','bank.branch_id=branch.id','left')
            ->view('three_level','three_level_name','bank.three_level_id=three_level.id','left')
            ->json(['answer'])
            ->where('bank.status',2)
            ->order('bank.three_level_id desc')
            ->limit(($page-1)*$limit,$limit)
            ->select();
        return json(['code'=>200,'data'=>$data]);
    }

    /**未解题库分页
     * @param Request $request
     * @return \think\response\Json
     */
    public function weishenghe(Request $request){
        $limit = $request->param('limit',10);
        $page  = $request->param('page',1);
        $data = Db::view('bank',true)
            ->view('classes','class_name','bank.classes_id=classes.id','left')
            ->view('branch','branch_name','bank.branch_id=branch.id','left')
            ->view('three_level','three_level_name','bank.three_level_id=three_level.id','left')
            ->json(['answer'])
            ->where('bank.status','<>',2)
            ->where('bank.status','<>',1)
            ->order('bank.three_level_id desc')
            ->limit(($page-1)*$limit,$limit)
            ->select();
        return json(['code'=>200,'data'=>$data]);
    }

    /**设置项分页
     * @param Request $request
     * @return \think\response\Json
     */
    public function system(Request $request){
        $limit = $request->param('limit',10);
        $page  = $request->param('page',1);
        $tiaojian  = $request->param('tiaojian',null);
        if($tiaojian){
            $data = Db::name('system')
                ->where('system_name','like',"%$tiaojian%")
                ->order('id desc')
                ->limit(($page-1)*$limit,$limit)
                ->select();
        }else{
            $data = Db::name('system')->order('id desc')->limit(($page-1)*$limit,$limit)->select();
        }

        return json(['code'=>200,'data'=>$data]);
    }

    /**前台用户分页
     * @param Request $request
     * @return \think\response\Json
     */
    public function user(Request $request){
        $tiaojian = $request->param('tiaojian',null);
        $page     = $request->param('page',1);
        $limit    = $request->param('limit',10);
        if($tiaojian){
            $data = User::get()->where('user.user|user.user_name|user.nickname','like',"%$tiaojian%")
                ->order('user.id desc')
                ->limit(($page-1)*$limit,$limit)
                ->select();
        }else{
            $data = User::get()
                ->order('user.id desc')
                ->limit(($page-1)*$limit,$limit)
                ->select();
        }
        return json(['code'=>200,'data'=>$data]);
    }

    /**用户消息分页
     * @param Request $request
     * @return \think\response\Json
     */
    public function news(Request $request){
        $tiaojian = $request->param('tiaojian',null);
        $page     = $request->param('page',1);
        $limit    = $request->param('limit',10);
        if($tiaojian){
            $data = News::get()->where('user.user|user.nickname|user.user_name','like',"%$tiaojian%")
                ->where('user.status',0)
                ->order('news.create_time desc')
                ->limit(($page-1)*$limit,$limit)
                ->select();
        }else{
            $data = News::get()
                ->where('user.status',0)
                ->order('news.create_time desc')
                ->limit(($page-1)*$limit,$limit)
                ->select();
        }
        return json(['code'=>200,'data'=>$data]);
    }

    /**用户流水分页
     * @param Request $request
     * @return \think\response\Json
     */
    public function forward(Request $request){
        $tiaojian = $request->param('tiaojian',null);
        $limit = $request->param('limit',10);
        $page = $request->param('page',1);
        $start_time = $request->param('start_time',null);
        $end_time = $request->param('end_time',null);
        $type     = $request->param('type',1);
        $status   = $request->param('status',0);
        if($start_time){
            $start_time = strtotime($start_time);
            if($end_time){
                $end_time = strtotime($end_time);
            }else{
                $end_time = time();
            }
        }else{
            if($end_time){
                $end_time = strtotime($end_time);
                $start_time = 0;
            }else{
                $start_time = 0;
                $end_time = time();
            }
        }
        $end_time = $end_time+86400;
        if($tiaojian){
            $data = Forward::get()
                ->where('user.user|user.user_name|user.nickname','like',"%$tiaojian%")
                ->where('forward.type',$type)
                ->where('forward.status',$status)
                ->where('forward.create_time','>',$start_time)
                ->where('forward.create_time','<',$end_time)
                ->order('forward.end_time desc')
                ->limit(($page-1)*$limit,$limit)
                ->select();
        }else{
//            return $start_time;
            $data = Forward::get()
                ->where('forward.type',$type)
                ->where('forward.status',$status)
                ->where('forward.create_time','>',$start_time)
                ->where('forward.create_time','<',$end_time)
                ->order('forward.end_time desc')
                ->limit(($page-1)*$limit,$limit)
                ->select();
        }
//        return $data;
        return json(['code'=>200,'data'=>$data]);
    }

    /**vip用户分页
     * @param Request $request
     * @return \think\response\Json
     */
    public function vipuser(Request $request){
        $tiaojian = $request->param('tiaojian',null);
        $limit = $request->param('limit',10);
        $page = $request->param('page',1);
        if($tiaojian){
            $data = User::get()
                ->where('user.vip','>',0)
                ->where('user.user|user.nickname|user.user_name|vip_price.vip_name|vip_price.vip_icon|vip_price.vip_price','like',"%$tiaojian%")
                ->order('vip_price.vip_icon desc')
                ->limit(($page-1)*$limit,$limit)
                ->select();
        }else{
            $data = User::get()
                ->where('user.vip','>',0)
                ->order('vip_price.vip_icon desc')
                ->limit(($page-1)*$limit,$limit)
                ->select();
        }
        return json(['code'=>200,'data'=>$data]);
    }
}